package com.yitong.base.dao.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.yitong.base.dao.api.ReadDao;
import com.yitong.base.domain.Page;
import com.yitong.base.domain.TreeModel;
import com.yitong.base.exception.DaoException;

@Repository("readDao")
public class DefaultReadDaoImpl extends SqlSessionDaoSupport implements ReadDao {

	public long count(String statement, Object parameter) throws DaoException {
		Object lg = getSqlSession().selectOne(statement, parameter);
		return lg == null ? 0 : (Long) lg;
	}

	public boolean exists(String statement, Object parameter) throws DaoException {
		try {
			Object obj = getSqlSession().selectList(statement, parameter);
			return obj == null ? false : true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	public List find(String statement, Object parameter) throws DaoException {
		return getSqlSession().selectList(statement, parameter);
	}

	public List find(String statement, Object parameter, int star, int end) throws DaoException {

		return getSqlSession().selectList(statement, parameter, new RowBounds(star, end));
	}

	public DataSource getDataSource() throws DaoException {
		return null;
	}

	public String getNameSpace(Object model) throws DaoException {
		String nameSpace = model.getClass().getSimpleName();
		int idx = nameSpace.lastIndexOf("Model");
		if (idx > 0) {
			nameSpace = nameSpace.substring(0, idx);
		}
		return nameSpace + ".";
	}

	public Object load(String statement, Object parameter) throws DaoException {
		return getSqlSession().selectOne(statement, parameter);
	}

	@Autowired
	public void setSqlSessionFactory0(SqlSessionFactory sqlSessionFactory) {
		super.setSqlSessionFactory(sqlSessionFactory);
	}

	@SuppressWarnings("unchecked")
	public Page findPage(String sql, String sqlCount, Object param, int pageNum, int pageSize) {
		Page page = null;
		if (null != param) {
			page = new Page();
			// int pageNum =
			// Integer.parseInt(param.get(Constant.UPLOAD_page).toString());
			page.setCurrentPage(pageNum);
			pageNum -= 1;
			if (pageNum < 0) {
				pageNum = 0;
			}
			long total = this.count(sqlCount, param);
			total = (total % pageSize) == 0l ? (total / pageSize) : ((total / pageSize) + 1);
			if (pageNum > total) {
				pageNum = (int) total;
			}

			int size = pageSize < 1 ? 1 : pageSize;
			int star = (int) (pageNum * size);
			int end = size;

			List<?> data = this.find(sql, param, star, end);

			page.setPageSize(size);
			page.setTotalPage(total);
			page.setData(data);

		}
		return page;
	}

	@SuppressWarnings("unchecked")
	public Page<?> findPage1(String sql, String sqlCount, Object param, int pageNum, int pageSize) {
		Page page = null;
		if (null != param) {
			page = new Page();
			int currentPage = pageNum;

			pageNum -= 1;
			if (pageNum < 0) {
				pageNum = 0;
				currentPage = pageNum + 1;
			}
			long totalNum = this.count(sqlCount, param);
			long totalPage = (totalNum % pageSize) == 0l ? (totalNum / pageSize) : ((totalNum / pageSize) + 1);
			if (pageNum >= totalPage) {
				pageNum = (int) totalPage;
				currentPage = pageNum;
			}

			int size = pageSize < 1 ? 1 : pageSize;
			int star = (int) (pageNum * size);
			int end = size;

			List<?> data = this.find(sql, param, star, end);

			page.setPageSize(size);
			page.setCurrentPage(currentPage);
			page.setTotalPage(totalPage);
			page.setTotalNum(totalNum);
			page.setData(data);

		}
		return page;
	}

	public void findTree(ArrayList<TreeModel> treeList, String paramString, Map param) {
		List<Object> leafInfo = (List<Object>) this.find(paramString, param);
		if (leafInfo != null && leafInfo.size() > 0) {
			for (int i = 0; i < leafInfo.size(); i++) {
				Map m = (Map) leafInfo.get(i);
				TreeModel treeLeaf = new TreeModel();
				treeLeaf.setId(Integer.parseInt((m.get("id") + "")));
				treeLeaf.setPid(Integer.parseInt((m.get("pId") + "")));
				treeLeaf.setTitle((m.get("title") + ""));
				treeList.add(treeLeaf);
				param.put("pId", Integer.parseInt((m.get("id") + "")));
				findTree(treeList, paramString, param);
			}
		} else {
			return;
		}
	}

	@SuppressWarnings(value={"rawtypes","unused"})
	public List<?> listPage(String sql, String sqlCount, Object param,
			int pageNum, int pageSize) {
		List<?> list = new ArrayList();
			if(null != param){
				int currentPage = pageNum;
				pageNum -= 1;
				if(pageNum <0){
					pageNum = 0;
					currentPage = pageNum + 1 ;
				}
				long totalNum = this.count(sqlCount, param);
				long totalPage = (totalNum%pageSize) == 0l?(totalNum/pageSize):((totalNum/pageSize)+1);
				if(pageNum>=totalPage){
					pageNum = (int) totalPage;
					currentPage = pageNum;
				}
			 
				int size =  pageSize <1?1:pageSize;
				int star = (int) (pageNum*size);
				int end = size;
				list  = this.find(sql, param, star, end);
		}
		return list;
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public Page findPage2(String sql, String sqlCount, Object param, int pageNum, int pageSize) {
		Page page = null;
		if(null != param){
			page = new Page();
			int currentPage = pageNum;
			
			pageNum -= 1;
			if(pageNum <0){
				pageNum = 0;
				currentPage = pageNum + 1 ;
			}
			long totalNum = this.count(sqlCount, param);
			long totalPage = (totalNum%pageSize) == 0l?(totalNum/pageSize):((totalNum/pageSize)+1);
			if(pageNum>=totalPage){
				pageNum = (int) totalPage;
				currentPage = pageNum;
			}			 
			int size =  pageSize <1?1:pageSize;
			int star = (int) (pageNum*size);
			int end = size;
			
			
		    ((Map)param).put("star", star);
		    ((Map)param).put("end", end);
			
			List<?> data  = this.find(sql, param);
			
			 
			page.setPageSize(size) ;
			page.setCurrentPage(currentPage);
			page.setTotalPage(totalPage);
			page.setTotalNum(totalNum);
			page.setData(data);
			
		}
		return page;
	}
	
}
